<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">提货人姓名</text>
			<input class="input" type="text" v-model="addressData.name" placeholder="提货人姓名"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">提货人手机号</text>
			<input class="input" type="number" v-model="addressData.phone" placeholder="提货人手机号码"
				placeholder-class="placeholder" />
		</view>

		<!-- <view class="row b-b">
			<text class="tit">所在地区</text>
			<input @click="chooseArea" class="input" type="text" v-model="addressData.area" placeholder="省、市、区、街道"
				placeholder-class="placeholder" />
		</view>

		<view class="row b-b">
			<text class="tit">详细地址</text>

			<input class="input" type="text" v-model="addressData.address" placeholder="小区楼栋/乡村名称"
				placeholder-class="placeholder" />
			<text @click="chooseLocation" class="yticon icon-shouhuodizhi"></text>
		</view> -->

		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.defaule" color="#fa436a" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm">提交</button>
		<regionWindow ref="region" @conceal="finish"></regionWindow>

	</view>
</template>

<script>
	import {
		addAddress
	} from "@/server/api.js"
	import regionWindow from '@/components/region-window/index.vue'
	export default {
		data() {
			return {
				popup: false,
				addressData: {
					name: '',
					phone: '',
					default: true
				}
			}
		},
		components: {
			regionWindow
		},
		onLoad(option) {
			let title = '新增提货人信息';
			if (option.type === 'edit') {
				title = '编辑提货人信息'

				this.addressData = JSON.parse(option.data)
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			switchChange(e) {
				this.addressData.default = e.detail.value;
			},
			finish(param) {

				this.addressData.area = param.province + param.city + param.area + param.street
				// 获取到传过来的 省 市 区 县数据
				console.log(param);
				// 选择完毕后隐藏
				this.popup = false;

			},
			chooseArea() {
				this.$refs.region.open()
			},
			//地图选择地址
			chooseLocation() {
				uni.chooseLocation({
					success: (data) => {
						this.addressData.addressName = data.name;
						this.addressData.address = data.name;
						this.getLocation(data.latitude, data.longitude)
					},
					fail: (res) => {
						console.log(res)
					}
				})
			},
			getLocation(latitude, longitude) {

				let vm = this;
				let locationObj = latitude + ',' + longitude;
				let url =
					'https://apis.map.qq.com/ws/geocoder/v1?coord_type=5&get_poi=1&output=jsonp&poi_options=page_size=1;page_index=1';
				/* 根据经纬度获取地址 */
				this.$jsonp(url, {
						key: '&key=KIJBZ-ZBMKK-AN7J4-A52JC-YN7S5-THFNY',
						location: locationObj
					})
					.then(res => {
						console.log("********")
						console.log(res)
						var adress = res.result.address
						console.log("地址：" + adress)

					})
					.catch(err => {});
			},
			conceal(param) {
				this.addressData = param
				this.$refs.region.close()

			},

		//提交
		confirm() {
			console.log("***********")
			let data = this.addressData;
			console.log(data);
			if (!data.name) {
				this.$api.msg('请填写收货人姓名');
				return;
			}
			if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.phone)) {
				this.$api.msg('请输入正确的手机号码');
				return;
			}
			// if (!data.address) {
			// 	this.$api.msg('请在地图选择所在位置');
			// 	return;
			// }
			// if (!data.area) {
			// 	this.$api.msg('请填写门牌号信息');
			// 	return;
			// }


				addAddress(data).then(data => {
					this.$api.msg(`地址${this.manageType=='edit' ? '修改': '添加'}成功`);
					uni.$emit("changeAddress", {
						dorm: ""
					})
					uni.navigateBack()
				})


			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-top: 16upx;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 180upx;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.input {
			margin-left: 10px;
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.icon-shouhuodizhi {
			font-size: 36upx;
			color: $font-color-light;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
